package com.xmb.wcapi.controller;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.crypto.asymmetric.SignAlgorithm;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xmb.wcapi.constant.R;
import com.xmb.wcapi.entity.Customer;
import com.xmb.wcapi.service.ICustomerService;
import com.xmb.wcapi.util.BeanUtil;
import com.xmb.wcapi.util.ShiroUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huangping
 * @since 2024-04-12
 */
@Controller
@RequestMapping("manage/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @PostMapping("/add")
    @ResponseBody
    public R add(String url, String obj) {
        Customer customer = BeanUtil.parseObjToBean(obj, Customer.class);
        if (null == customer){
            return R.error();
        }
        if (StringUtils.isEmpty(customer.getCusCode())){
            return R.error("添加失败！");
        }
        customer.setCreateUser(ShiroUtil.getUserId());
        customer.setCreateTime(DateTime.now());
        if (null == customer.getPlaintextPwd()){
            customer.setPwd(DigestUtil.md5Hex("123456"));
        }else {
            customer.setPwd(DigestUtil.md5Hex(customer.getPlaintextPwd()));
        }
        //查询是否存在
        long count = customerService.count(Wrappers.lambdaQuery(Customer.class).eq(Customer::getCusCode, customer.getCusCode()));
        if (count > 0){
            return R.error("重复添加！");
        }
        customerService.save(customer);
        return R.ok();
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(String obj) {
        Customer customer = BeanUtil.parseObjToBean(obj, Customer.class);
        if (null == customer){
            return R.error();
        }
        if (StringUtils.isEmpty(customer.getCusCode())){
            return R.error("修改失败！");
        }
        customer.setCreateUser(ShiroUtil.getUserId());
        customer.setCreateTime(DateTime.now());
        if (null == customer.getPlaintextPwd()){
            customer.setPwd(DigestUtil.md5Hex("123456"));
        }else {
            customer.setPwd(DigestUtil.md5Hex(customer.getPlaintextPwd()));
        }
        //查询是否存在
        long count = customerService.count(Wrappers.lambdaQuery(Customer.class).eq(Customer::getCusCode, customer.getCusCode()).notIn(Customer::getCusCode, customer.getCusCode()));
        if (count > 0){
            return R.error("重复修改！");
        }
        customerService.updateById(customer);
        return R.ok();
    }

    @GetMapping("/addHtml")
    public String addHtml() {
        return "customer/add";
    }

}

